package org.airline.impl;

import java.util.List;

import org.airline.dao.IUserDao;
import org.airline.entity.PageBean;
import org.airline.entity.User;
import org.springframework.stereotype.Component;
@SuppressWarnings("all")
@Component("UserDaoImpl")
public class UserDaoImpl extends HibernateDaoImpl implements IUserDao{

	// 检测用户名是否已被注册
	@Override
	public boolean checkUserName(String userName) throws Exception {
		String hql = "FROM User user WHERE user.userName = '" + userName + "'";
		List<User> userList = (List<User>)super.find(hql);
		if (null != userList && userList.size() > 0) {
			return true;
		}
		return false;
	}

	// 检测邮箱是否已被注册
	@Override
	public boolean checkEamil(String email) throws Exception {
		String hql = "FROM User user WHERE user.email = '" + email + "'";
		List<User> userList = (List<User>)super.find(hql);
		if (null != userList && userList.size() > 0) {
			return true;
		}
		return false;
	}

	// 持久化用户
	@Override
	public boolean saveUser(User user) throws Exception {
		super.save(user) ;
		return false;
	}
	//检查用户是否存在
	@Override
	public User checkUser(String userName, String password) throws Exception {
		String hql = "FROM User user WHERE user.userName = ? AND user.password = ?" ;
		return (User)super.get(hql, new String[]{userName, password});
	}
	
	// 检查管理是否存在
	@Override
	public User checkAdminUser(String userName, String password)
			throws Exception {
		String hql = "FROM User user WHERE user.userName = ? AND user.password = ? AND user.rank = 1" ;
		return (User)super.get(hql, new String[]{userName, password});
	}

	
	//修改用户信息
	@Override
	public void updateUser(User user) throws Exception {
		super.update(user);
	}
	//根据id删除用户信息
	@Override
	public void deleteUserById(int id) throws Exception {
		super.delete(User.class,id);
	}
	//查询所有用户信息
	@Override
	public List<User> getAllUser() throws Exception {
		return (List<User>)super.getHibernateTemplate().find("from user");
	}
	//根据id获取用户信息
	@Override
	public User getUserInfoById(int id) throws Exception {
		return (User)super.get(User.class, id);
	}
	//根据关键字查询用户信息
	@Override
	public List<User> getUsersByWords(String keyWord) throws Exception {
		String hql = "FROM User "
			+ "WHERE userName LIKE '%" + keyWord + "%' "
			+ "OR trueName LIKE '%" + keyWord + "%' "
			+ "OR tel LIKE '%" + keyWord + "%' "
			+ "OR sex LIKE '%" + keyWord + "%' "
			+ "OR regDate LIKE '%" + keyWord + "%' "
			+ "OR email LIKE '%" + keyWord + "%' "
			+ "OR address LIKE '%" + keyWord + "%' ";
		return (List<User>)super.getHibernateTemplate().find(hql);
	}
	//分页查询
	@Override
	public PageBean queryForPage(int pageSize, int page) throws Exception {
		final String hql = "FROM User ORDER BY id";
		return super.queryForPage(hql, pageSize, page);
	}
	//根据关键字分页查询
	@Override
	public PageBean queryForPageByKeyWords(String keyWord, int pageSize, int page)
			throws Exception {
	String hql = "FROM User "
			+ "WHERE userName LIKE '%" + keyWord + "%' "
			+ "OR trueName LIKE '%" + keyWord + "%' "
			+ "OR tel LIKE '%" + keyWord + "%' "
			+ "OR sex LIKE '%" + keyWord + "%' "
			+ "OR email LIKE '%" + keyWord + "%' "
			+ "OR address LIKE '%" + keyWord + "%' ";
	return super.queryForPage(hql, pageSize, page);
	}

}
